#include "DirectShow.h"

DirectShow::DirectShow()
	: m_pGraphBuilder(NULL)
	, m_pMediaControl(NULL)
	, m_pMediaEvent(NULL)
	, m_pVideoWindow(NULL)
	, isPlaying(true)
	, isFinished(false)
	, bInitialized(false)
{}

bool DirectShow::Init(HWND hWnd)
{
	m_hWnd = hWnd;

	CoInitialize(NULL);

	CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
						IID_IGraphBuilder, (void**)&m_pGraphBuilder);

	m_pGraphBuilder->QueryInterface(IID_IMediaControl, (void**)&m_pMediaControl);	// setup the media controller
	m_pGraphBuilder->QueryInterface(IID_IMediaEvent, (void**)&m_pMediaEvent);		// setup the media event handler
	//m_pGraphBuilder->RenderFile(L"File Name Here.wmv", NULL);						// load the video file

	m_pMediaControl->QueryInterface(IID_IVideoWindow, (void**)&m_pVideoWindow);		// create the video window


	////// Begin Setup of the window //////
	m_pVideoWindow->put_Owner((OAHWND)m_hWnd);										// set the window to our hWnd handle
	m_pVideoWindow->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE);		// set the style (?)
	RECT rClientRect;
	GetClientRect(m_hWnd, &rClientRect);											// obtain the window dimensions
	m_pVideoWindow->SetWindowPosition(rClientRect.left, rClientRect.top,			// set the size of the video to the size of the screen
										rClientRect.right, rClientRect.bottom);

	/////// End Setup of the window ///////

	return true;
}

DirectShow::~DirectShow()
{
	// release memory from pointers unless they have NULL values

	if(m_pMediaControl)
		m_pMediaControl->Release();
	if(m_pMediaEvent)
		m_pMediaEvent->Release();
	if(m_pGraphBuilder)
		m_pGraphBuilder->Release();
	if(m_pVideoWindow)
		m_pVideoWindow->Release();	
}